package com.tsmti.util;

import com.tsmti.core.common.constant.RedisConstants;
import com.tsmti.core.util.ConvertUtil;
import com.tsmti.core.util.RedisUtils;
import com.tsmti.core.util.SpringContextUtils;
import com.tsmti.core.util.StringUtils;
import com.tsmti.system.entity.DataDictionary;
import com.tsmti.system.service.DataDictionaryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Package：com.tsmti.util
 * @Class：DictUtils
 * @Description： 字典工具类
 * @Author：zcw
 * @Date：Created in 2018/5/17 0:06
 * @Company:
 * @Version：
 * @Modified By:
 */
public class DictionaryUtils {
    private static Logger logger = LoggerFactory.getLogger(DictionaryUtils.class);
    private static DataDictionaryService dataDictionaryService = SpringContextUtils.getBean(DataDictionaryService.class);


    /**
     * 根据值获取显示名称
     *
     * @param groupCode 分类编码
     * @param value     值
     * @return
     */
    public static String getSelectCode(String groupCode, String value) {
        return getSelectCode(groupCode, value, value);
    }

    /**
     * 根据值获取显示名称
     *
     * @param groupCode    分类编码
     * @param value        值
     * @param defaultValue 默认值
     * @return 返回字典显示名称
     */
    public static String getSelectCode(String groupCode, String value, String defaultValue) {
        List<DataDictionary> dictionaryList = getDictList(groupCode);
        if (dictionaryList != null && dictionaryList.size() > 0) {
            if (StringUtils.isNotBlank(groupCode) && StringUtils.isNotBlank(value)) {
                for (DataDictionary dict : dictionaryList) {
                    if (groupCode.equals(dict.getGroupCode()) && value.equals(dict.getCode())) {
                        return dict.getName();
                    }
                }
            }
        }
        return defaultValue;
    }

    /**
     * 获取具体的字典值
     *
     * @param groupCode
     * @param name
     * @return
     */
    public static String getSelectValue(String groupCode, String name) {
        List<DataDictionary> dictionaryList = getDictList(groupCode);
        if (dictionaryList != null && dictionaryList.size() > 0) {
            if (StringUtils.isNotBlank(groupCode) && StringUtils.isNotBlank(name)) {
                for (DataDictionary dict : dictionaryList) {
                    if (groupCode.equals(dict.getGroupCode()) && name.equals(dict.getName())) {
                        return dict.getCode();
                    }
                }
            }
        }
        return name;
    }

    /**
     * 根据值获取字典对象
     *
     * @param groupCode
     * @param code
     * @return
     */
    public static DataDictionary getDictionary(String groupCode, String code) {
        List<DataDictionary> dictionaryList = getDictList(groupCode);
        if (StringUtils.isNotBlank(groupCode) && StringUtils.isNotBlank(code)) {
            for (DataDictionary dict : dictionaryList) {
                if (groupCode.equals(dict.getGroupCode()) && code.equals(dict.getCode())) {
                    return dict;
                }
            }
        }
        return null;
    }

    /**
     * 字段转换成grid选择项
     *
     * @param groupCode
     * @param type 1:value为code 文本为name 2:value为code 文本为code
     *      *             3:value为name 文本为code  4:value为name 文本为name
     * @return
     */
    public static String getGridSelect(String groupCode,Integer type) {
        String selectValue = "";
        List<DataDictionary> dictionaryList = getDictList(groupCode);
        for (DataDictionary info : dictionaryList) {
            if (type == 1) {
                selectValue += ConvertUtil.null2String(info.getCode()) + ":" + ConvertUtil.null2String(info.getName()) + ";";
            } else if (type == 2) {
                selectValue += ConvertUtil.null2String(info.getCode()) + ":" + ConvertUtil.null2String(info.getCode()) + ";";
            } else if (type == 3) {
                selectValue += ConvertUtil.null2String(info.getName()) + ":" + ConvertUtil.null2String(info.getCode()) + ";";
            } else if (type == 4) {
                selectValue += ConvertUtil.null2String(info.getName()) + ":" + ConvertUtil.null2String(info.getName()) + ";";
            }
        }

        if(!selectValue.isEmpty()){
            selectValue = selectValue.substring(0,selectValue.length()-1);
        }
        return selectValue;
    }

    /**
     * 将数据字典组装成选择项
     * @param groupCode
     * @param type 1:value为code 文本为name 2:value为code 文本为code
     *             3:value为name 文本为code  4:value为name 文本为name
     * @return
     */
    public static String getSelectOption(String groupCode,Integer type) {
        String option = "<select>";
        List<DataDictionary> dictionaryList = getDictList(groupCode);
        for (DataDictionary info : dictionaryList) {
            if (type == 1) {
                option += "<option value='" + ConvertUtil.null2String(info.getCode()) + "'>" + ConvertUtil.null2String(info.getName()) + "</option>";
            } else if (type == 2) {
                option += "<option value='" + ConvertUtil.null2String(info.getCode()) + "'>" + ConvertUtil.null2String(info.getCode()) + "</option>";
            } else if (type == 3) {
                option += "<option value='" + ConvertUtil.null2String(info.getName()) + "'>" + ConvertUtil.null2String(info.getCode()) + "</option>";
            } else if (type == 4) {
                option += "<option value='" + ConvertUtil.null2String(info.getName()) + "'>" + ConvertUtil.null2String(info.getName()) + "</option>";
            }
        }
        option +="</select>";
        return option;
    }

    public static List<DataDictionary> getDictList(String groupCode) {
        Map<String, List<DataDictionary>> allMap = null;
        try {
            allMap = (Map<String, List<DataDictionary>>) RedisUtils.getObject(RedisConstants.DICTIONARY_CACHE);
        } catch (Exception e) {
            logger.warn("读取字典缓存失败!");
            e.printStackTrace();
        }

        List<DataDictionary> dictionaryList = null;
        if (allMap != null && allMap.size() > 0) {
            dictionaryList = allMap.get(groupCode);
            if (dictionaryList == null) {
                dictionaryList = new ArrayList<DataDictionary>();
            }
            return dictionaryList;
        }
        return new ArrayList<DataDictionary>();
    }

    public static Map<String, String> getDictGrouopMap() {
        Map<String, List<DataDictionary>> allMap = null;
        Map<String, String> dictMap = new HashMap<String, String>(16);
        try {
            allMap = (Map<String, List<DataDictionary>>) RedisUtils.getObject(RedisConstants.DICTIONARY_CACHE);
            for (String key : allMap.keySet()) {
                if (null != allMap.get(key).get(0)) {
                    dictMap.put(key, allMap.get(key).get(0).getDictGroup().getName());
                }
            }
        } catch (Exception e) {
            logger.warn("读取字典缓存失败!");
            e.printStackTrace();
        }
        return dictMap;
    }
}
